package com.wanchuang.hepos.ui.page.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.request.HomeRequestViewModel;
import com.wanchuang.hepos.bridge.state.home.SheBeiViewModel;
import com.wanchuang.hepos.databinding.FragmentSheBeiBinding;
import com.wanchuang.hepos.databinding.FragmentZhanDanBinding;
import com.wanchuang.hepos.ui.base.BaseFragment;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class SheBeiFragment extends BaseFragment {


    private FragmentSheBeiBinding mBinding;
    private SheBeiViewModel mViewModel;
    private HomeRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(SheBeiViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(HomeRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_she_bei, container, false);
        mBinding = FragmentSheBeiBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getBind().observe(this, bind -> {
            hideProgress();
            mViewModel.adapter.get().addData(bind);
            mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
        });


        mRequestViewModel.get_sn();
    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
            mSharedViewModel.isHomeLoad.setValue(true);
        }


        public void itemOnClick(int position) {


        }

        public void loadMoreListener() {

        }
    }
}
